/**
 *
 */
package com.csun.cmny.provider.controller;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.Group;
import com.csun.cmny.provider.dto.NursingBindRoomDto;
import com.csun.cmny.provider.entity.Page;
import com.csun.cmny.provider.model.dto.user.OmcUserQueryDto;
import com.csun.cmny.provider.model.vo.OmcUserVo;
import com.csun.cmny.provider.pojo.Bed;
import com.csun.cmny.provider.pojo.NursingGroup;
import com.csun.cmny.provider.pojo.Room;
import com.csun.cmny.provider.pojo.User;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcGroupService;
import com.csun.cmny.provider.utils.MapUtils;
import com.csun.cmny.provider.utils.ValidateUtil;
import com.csun.cmny.provider.validator.GroupValid;
import com.csun.cmny.provider.vo.NurseHomeVo;
import com.csun.cmny.provider.vo.NursingGroupBind;
import com.csun.cmny.provider.vo.NursingGroupVo;
import com.csun.cmny.provider.vo.OmcUserDetailVo;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author chenzheng
 * @date 2019年7月15日
 * @time 上午9:52:36
 * Description : 护工管理
 */
@RestController
@RequestMapping("/group")
public class OmcGroupController extends BaseController {
    @Resource
    private OmcGroupService groupService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * 查询该机构的所有没有分配组的护工
     */

    @RequestMapping(value = "/selectShowUn", method = RequestMethod.POST)
    public Wrapper selectShowUn(
    ) {

        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Group group = new Group();
        group.setGroupId(loginAuthDto.getGroupId());


        List<OmcUserVo> list = groupService.selectUnUnrsingUserListByGroupId(group);
        if (null == list || list.isEmpty()) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.not.exist.nursing", null), 0);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("group.allot.fail", null), list);
    }

    /**
     * 添加班组和批量给组添加成员
     */


    @RequestMapping(value = "/groupAdd", method = RequestMethod.POST)
    public Wrapper groupAdd(
            @RequestBody @Validated(value = {GroupValid.class}) Group group, BindingResult br
    ) {


        //参数校验
        String[] validateParam = {"name", "userId"};
        Map<String, String> errorMap = ValidateUtil.getErrorMap(br, validateParam);
        if (errorMap != null && errorMap.size() != 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, MapUtils.getFirstOrNull(errorMap), null);
        }
        List<User> list = group.getUserList();
        List<Long> userList = new ArrayList<>();
        List<Long> dtoList = new ArrayList<>();

//        List<OmcUserVo> userListWithoutGroup = groupService.selectUnUnrsingUserListByGroupId(group);
//        if(userListWithoutGroup == null || userListWithoutGroup.size()<1){
//            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Group.nursing.user.is.not.nursing", null), null);
//        }

        if (!CollectionUtils.isEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                if (null == list.get(i).getUserid() || list.get(i).getUserid() > 92233720368547758071F || list.get(i).getUserid() <= 1L) {
                    errorMap.put("userid", cmnyMessageSource.getMessage("group.user.id.error", null));
                    return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, MapUtils.getFirstOrNull(errorMap), null);
                }
                userList.add(list.get(i).getUserid());

            }
        }else{
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("group.users.null", null), null);
        }
        dtoList.addAll(userList);
        //判断绑定的护工是否有不是护工角色的用户
        OmcUserQueryDto omcUserQueryDto = new OmcUserQueryDto();
        omcUserQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        omcUserQueryDto.setUserIdList(dtoList);
        List<Long> notNursing = groupService.isNotNursing(omcUserQueryDto);
        if (notNursing.size() > 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Group.nursing.user.is.not.nursing", null), notNursing);
        }
        //判断是否有需要绑定护工组的护工
        if (null == userList || userList.size() <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("group.nursing.bind.user.is.null", null), notNursing);
        }
        //判断需绑定的护工集合是否有护工已被绑定
        if (userList.size() > 0) {
            for (Long userId : userList) {
                Long nursingGroupId = groupService.selectNuringGroupIdByUserId(userId);
                if (null != nursingGroupId && nursingGroupId > 0) {
                    return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Group.nursing.user.isbind", null), null);
                }
            }

        }
        //通过班组名称和机构id查询
        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        group.setGroupId(loginAuthDto.getGroupId());

        group.setUserName(loginAuthDto.getUserName());
        int n = groupService.selectByName(group);//判断该班组名称是否存在，不存在则添加，存在则不添加
        if (n <= 0) {
            group.setModifier(loginAuthDto.getUserId());
            int m = groupService.insert(group);//添加班组
            if (m <= 0) {

                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.add.fail", null), 0);
            }
            Long groupId = group.getNursingGroupId();

            if (!CollectionUtils.isEmpty(list)) {
                for (User user : list) {
                    user.setGroupId(groupId);
                }
                int num = groupService.insertNursingGroupBind(list);
                if (num <= 0) {

                    return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.add.nursing.fail", null), 0);
                }
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.add.success", null), n);
        }
        return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.name.exist", null), 0);
    }

    /**
     * 给组批量添加护工
     */


    @RequestMapping(value = "/groupAddNurse", method = RequestMethod.POST)
    public Wrapper groupAddNurse(
            @RequestBody @Validated(value = {GroupValid.class}) Group group, BindingResult br
    ) {

        //参数校验
        String[] validateParam = {"nursingGroupId"};
        Map<String, String> errorMap = ValidateUtil.getErrorMap(br, validateParam);
        if (errorMap != null && errorMap.size() != 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, MapUtils.getFirstOrNull(errorMap), null);
        }

        //查询是否有未绑定护工组的护工
        Group groupQuery = new Group();
        group.setGroupId(getLoginAuthDto().getGroupId());
        List<OmcUserVo> unSelectedUserlist = groupService.selectUnUnrsingUserListByGroupId(group);
        if (null == unSelectedUserlist || unSelectedUserlist.isEmpty()) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.not.exist.nursing", null), 0);
        }

        List<User> list = group.getUserList();
        List<Long> userIdList = new ArrayList<>();
        List<Long> dtoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                if (null == list.get(i).getUserid() || list.get(i).getUserid() > 92233720368547758071f || list.get(i).getUserid() <= 1L) {
                    return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("group.user.id.error", null), null);
                }
                userIdList.add(list.get(i).getUserid());
            }
        }
        dtoList.addAll(userIdList);
        //判断绑定的护工是否有不是护工角色的用户
        OmcUserQueryDto omcUserQueryDto = new OmcUserQueryDto();
        omcUserQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        omcUserQueryDto.setUserIdList(dtoList);
        List<Long> notNursing = groupService.isNotNursing(omcUserQueryDto);
        if (notNursing.size() > 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Group.nursing.user.is.not.nursing", null), notNursing);
        }
        //判断是否有需要绑定护工组的护工
        if (null == userIdList || userIdList.size() <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("group.nursing.bind.user.is.null", null), notNursing);
        }
        //判断需绑定的护工集合是否有护工已被绑定
        if (userIdList.size() > 0) {
            for (Long userId : userIdList) {
                Long nursingGroupId = groupService.selectNuringGroupIdByUserId(userId);
                if (null != nursingGroupId && nursingGroupId > 0) {
                    return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Group.nursing.user.isbind", null), null);
                }
            }

        }
        Long groupId = group.getNursingGroupId();
        int num = 0;
        if (!CollectionUtils.isEmpty(list)) {
            for (User user : list) {
                user.setGroupId(groupId);
            }
            num = groupService.insertNursingGroupBind(list);
            if (num <= 0) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.add.nursing.fail", null), num);
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.add.success", null), num);
        }
        return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.add.nursing.fail", null), num);
    }

    /**
     * 删除护工组
     */
    @RequestMapping(value = "/groupDelById", method = RequestMethod.POST)
    public Wrapper groupDelById(Long nursingGroupId) {
        String msg_group_delete_param_error = cmnyMessageSource.getMessage("group.delete.param.error", null);
        String msg_comment_delete_sucess = cmnyMessageSource.getMessage("comment.delete.sucess", null);
        String msg_comment_delete_fail = cmnyMessageSource.getMessage("comment.delete.fail", null);

        if (null == nursingGroupId || nursingGroupId <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, msg_group_delete_param_error, null);
        }

        int n = groupService.deleteById(nursingGroupId);//删除组
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, msg_comment_delete_fail, n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, msg_comment_delete_sucess, n);
    }

    /**
     * 查询登录用户的护工组ID
     */
    @RequestMapping(value = "/selectNursingGroupId", method = RequestMethod.POST)
    public Wrapper selectNursingGroupId() {
        Long n = groupService.selectNuringGroupIdByUserId(getLoginAuthDto().getUserId());
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.query.nursing.group.id.fail", null), n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), n);
    }

    /**
     * 调组或者移除护工
     *
     * @param group
     * @return
     */
    @RequestMapping(value = "/updateByUserId", method = RequestMethod.POST)
    public Wrapper updateByUserId(Group group) {
        if (null == group.getUserId() || group.getUserId() <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("group.remove.param.error", null), null);
        }
        //判断要移组或者移除的护工是否为班长
        NursingGroup nursingGroupDto = new NursingGroup();
        nursingGroupDto.setGroupId(getLoginAuthDto().getGroupId());
        nursingGroupDto.setUserId(group.getUserId());
        int num = groupService.selectCount(nursingGroupDto);
        if (num > 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.user.not.master", null), 0);
        }
        if (group.getNursingGroupId() != null && group.getNursingGroupId() > 0) {
            NursingGroup nursingGroup = groupService.selectByKey(group.getNursingGroupId());
            if (null == nursingGroup) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.is.not.exist", null), null);
            }
        }
        //根据护工ID删除护工组绑定关系
        Group groupDto = new Group();
        groupDto.setUserId(group.getUserId());
        int n = groupService.deleteNursingGroupBind(groupDto);
        if (group.getNursingGroupId() != null && group.getNursingGroupId() > 0) {
            List<User> list = new ArrayList<>();
            User user = new User();
            user.setGroupId(group.getNursingGroupId());
            user.setUserid(Long.valueOf(group.getUserId()));
            list.add(user);
            n = groupService.insertNursingGroupBind(list);
        }
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.remove.fail", null), n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("group.remove.success", null), n);
    }

    /**
     * 修改护工组名称
     */


    @RequestMapping(value = "/updateNameById", method = RequestMethod.POST)
    public Wrapper updateNameById(
            @Validated(value = {GroupValid.class}) Group group, BindingResult br
    ) {

        String[] validateParam = {"name", "nursingGroupId"};
        Map<String, String> errorMap = ValidateUtil.getErrorMap(br, validateParam);
        if (errorMap != null && errorMap.size() != 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, MapUtils.getFirstOrNull(errorMap), null);
        }
        if (group.getNursingGroupId() != null && group.getNursingGroupId() > 0) {
            NursingGroup nursingGroup = groupService.selectByKey(group.getNursingGroupId());
            if (null == nursingGroup) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.is.not.exist", null), null);
            }
        }

        int n = groupService.selectByName(group);
        if (n > 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.name.exist", null), 0);
        }

        LoginAuthDto loginAuthDto = getLoginAuthDto();

        group.setModifier(loginAuthDto.getUserId());
        group.setUserName(loginAuthDto.getUserName());
        //进入修改
        int m = groupService.updateById(group);
        if (m <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.update.fail", null), 0);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("group.update.success", null), n);
    }

    /**
     * 修改护工组班长
     */
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public Wrapper updateById(
            Group group
    ) {
        String[] validateParam = {"userId", "nursingGroupId"};

        List<NursingGroupBind> nursingGroupBindList = groupService.selectNursingGroupBind(group);
        if (nursingGroupBindList.size() <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("group.monitor.update.error", null), null);
        }
        //判断绑定的护工是否有不是护工角色的用户
        List<Long> userList = new ArrayList<>();
        userList.add(group.getUserId());
        OmcUserQueryDto omcUserQueryDto = new OmcUserQueryDto();
        omcUserQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        omcUserQueryDto.setUserIdList(userList);
        List<Long> notNursing = groupService.isNotNursing(omcUserQueryDto);
        if (notNursing.size() > 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Group.nursing.user.is.not.nursing", null), notNursing);
        }
        if (group.getNursingGroupId() != null && group.getNursingGroupId() > 0) {
            NursingGroup nursingGroup = groupService.selectByKey(group.getNursingGroupId());
            if (null == nursingGroup) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.is.not.exist", null), null);
            }
        }
        //用户绑定的护工组ID
        Long nursingGroupId = groupService.selectNuringGroupIdByUserId(group.getUserId());
        if (null == nursingGroupId || 0 == nursingGroupId.longValue() || nursingGroupId.longValue() != group.getNursingGroupId().longValue()) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.nursing.is.not.bind", null), null);
        }

        // int m = groupService.updateById(group);
        int m = groupService.updateUserIdByGroupId(group);
        if (m <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.monitor.update.fail", null), 0);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("group.monitor.update.success", null), m);
    }

    /**
     * 通过groupId查询护工组的所有护工
     */


    @RequestMapping(value = "/selectById", method = RequestMethod.POST)
    public Wrapper selectById(@RequestBody Map<String, String> queryParam
    ) {
        if (null == queryParam.get("nursingGroupId") || "".equals(queryParam.get("nursingGroupId"))) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("group.query.param.error", null), null);
        }
        Long groupId = Long.valueOf(queryParam.get("nursingGroupId"));
        if (null == groupId || groupId <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("group.query.param.error", null), null);
        }

        // 获取分页数据
        String pageNo = queryParam.get("pageNo");
        String pageSize = queryParam.get("pageSize");

        if (StringUtils.isBlank(pageNo)) {
            pageNo = "1";
        }
        if (StringUtils.isBlank(pageSize)) {
            pageSize = "10";
        }
        List<OmcUserVo> list = groupService.selectByGroupId(groupId);
        //分页数据
        Page page = new Page(Integer.parseInt(pageNo), Integer.parseInt(pageSize), list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), page);
    }


    /**
     * 通过homeId查询所有护工组信息
     * 或者模糊查询护工组
     */


    @RequestMapping(value = "/selectShowAll", method = RequestMethod.POST)
    public Wrapper selectShowAll(String name
    ) {
        if (name != null && name.length() > 32) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("group.name.length", null), null);
        }
        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Group group = new Group();
        group.setGroupId(loginAuthDto.getGroupId());
        group.setName(name);
        List<NursingGroupVo> list = groupService.selectShowAll(group);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("group.query.all.success", null), list);
    }

    /**
     * 通过homeId查询所有护工组的护工信息
     */


    @RequestMapping(value = "/selectAllNurse", method = RequestMethod.POST)
    public Wrapper selectAllNurse(
            @RequestBody Map<String, String> queryParam) {


        // 获取分页数据
        String pageNo = queryParam.get("pageNo");
        String pageSize = queryParam.get("pageSize");

        if (StringUtils.isBlank(pageNo)) {
            pageNo = "1";
        }
        if (StringUtils.isBlank(pageSize)) {
            pageSize = "10";
        }
        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Group group = new Group();
        group.setGroupId(loginAuthDto.getGroupId());
        List<NursingGroupVo> list = groupService.selectNursingListByName(group);
        //分页数据
        Page page = new Page(Integer.parseInt(pageNo), Integer.parseInt(pageSize), list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("group.query.all.success", null), page);
    }


    /**
     * 通过buildingId查询该机构没有被分配的房间
     */
    @RequestMapping(value = "/selectShowUnRoom", method = RequestMethod.POST)
    public Wrapper selectShowUnRoom(Long buildingId
    ) {
        String msg_group_select_bulid_param_error = cmnyMessageSource.getMessage("group.select.bulid.param.error", null);
        String msg_group_bulid_not_exist = cmnyMessageSource.getMessage("group.bulid.not.exist", null);
        String msg_comment_query_success = cmnyMessageSource.getMessage("comment.query.success", null);

        if (null == buildingId || buildingId <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, msg_group_select_bulid_param_error, null);
        }
        List<Room> list = groupService.selectByBuildingId(buildingId);
        if (CollectionUtils.isEmpty(list)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, msg_group_bulid_not_exist, null);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, msg_comment_query_success, list);
    }
    /**
     * 1.查询所有楼栋-通过homeId查询所有楼栋Building/buildingShowAll（调用该接口BuildingController）
     * 2.根据buildingId和floorNo查询房间Room/roomShowAll（调用该接口RoomController）
     */

    /**
     * 给组批量添加房间
     */

    @RequestMapping(value = "/groupAddRoom", method = RequestMethod.POST)
    public Wrapper groupAddRoom(
            @RequestBody @Validated(value = {GroupValid.class}) Group group, BindingResult br
    ) {


        //参数校验
        String[] validateParam = {"nursingGroupId"};
        Map<String, String> errorMap = ValidateUtil.getErrorMap(br, validateParam);
        if (errorMap != null && errorMap.size() != 0) {
            //* 返回参数校验错误详情 *//*
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, MapUtils.getFirstOrNull(errorMap), null);
        }
        //判断护工组是否存在
        NursingGroup nursingGroup = groupService.selectByKey(group.getNursingGroupId());
        if (null == nursingGroup) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.is.not.exist", null), null);
        }

        List<Room> list = group.getRoomList();
        Long nursingGroupId = group.getNursingGroupId();
        int num = 0;
        if (!CollectionUtils.isEmpty(list)) {
            for (Room room : list) {
                room.setNursingGroupId(nursingGroupId);
            }
            num = groupService.insertNursingRoomBind(list);
            if (num <= 0) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("group.add.room.fail", null), 0);
            }
            //更新remark
            List<Room> buildingList = groupService.selectForBuilding(group);//取得楼栋集合
            StringBuilder remark = new StringBuilder("");
            StringBuilder floor = new StringBuilder("");//楼层
            Set<Integer> buildingIdSet = new HashSet<>();

                for (Room room : buildingList) {

                    if (PublicUtil.isNotEmpty(room.getBuildingId())){
                        buildingIdSet.add(room.getBuildingId());
                    }
                }

            for (Integer buildingId : buildingIdSet) {
                int temp = -1;
                for (int i = 0; i <= buildingList.size() - 1; i++) {
                    if (buildingId.equals(buildingList.get(i).getBuildingId())) {
                        floor.append(buildingList.get(i).getFloorNo() + "层");
                        temp = i;
                    }
                }
                if (temp >= 0) {
                    remark.append(buildingList.get(temp).getBuildingName());
                }
                remark.append(floor);
                floor.delete(0, floor.length());
            }
            group.setRemark(remark.toString());
            int n = groupService.updateById(group);//进入更新
//    		return new JsonResult(true,"给护工组添加房间成功",num);
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("group.add.room.succcess", null), num);
        }
        return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("group.add.room.param.error", null), num);
    }

    /**
     * 批量移除房间
     */
    @RequestMapping(value = "/groupDelRoom", method = RequestMethod.POST)
    public Wrapper groupDelRoom(
            @RequestBody @Validated(value = {GroupValid.class}) Group group, BindingResult br
    ) {

        String msg_comment_delete_param_error = cmnyMessageSource.getMessage("comment.delete.param.error", null);
        String msg_group_room_id_error = cmnyMessageSource.getMessage("group.room.id.error", null);
        String msg_comment_delete_sucess = cmnyMessageSource.getMessage("comment.delete.sucess", null);
        String msg_comment_delete_fail = cmnyMessageSource.getMessage("comment.delete.fail", null);

        //参数校验
        String[] validateParam = {"nursingGroupId"};
        Map<String, String> errorMap = ValidateUtil.getErrorMap(br, validateParam);
        if (errorMap != null && errorMap.size() != 0) {
            //* 返回参数校验错误详情 *//*
//            return new JsonResult(false, "移除房间参数错误", errorMap);
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, msg_comment_delete_param_error, null);
        }

        List<Room> list = group.getRoomList();
        if (CollectionUtils.isEmpty(list)) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, msg_comment_delete_param_error, null);
        }
        int num = groupService.deleteNursingRoomBind(list, group.getNursingGroupId());
        if (num <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, msg_comment_delete_fail, num);
        }
        //更新remark
        List<Room> buildingList = groupService.selectForBuilding(group);//取得楼栋集合
        StringBuilder remark = new StringBuilder("");
        StringBuilder floor = new StringBuilder("");//楼层
        Set<Integer> buildingIdSet = new HashSet<>();
        for (Room room : buildingList) {
            if (null != room.getBuildingId()) {
                buildingIdSet.add(room.getBuildingId());
            }
        }
        for (Integer buildingId : buildingIdSet) {
            int temp = -1;
            for (int i = 0; i <= buildingList.size() - 1; i++) {
                if (buildingId.equals(buildingList.get(i).getBuildingId())) {
                    floor.append(buildingList.get(i).getFloorNo() + "层");
                    temp = i;
                }
            }
            if (temp >= 0) {
                remark.append(buildingList.get(temp).getBuildingName());
            }
            remark.append(floor);
            floor.delete(0, floor.length());
        }

        group.setRemark(remark.toString());
        int n = groupService.updateById(group);//进入更新
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, msg_comment_delete_sucess, num);
    }

    /**
     * 通过groupId查询护工组的所有房间——护工组管理
     * 显示房间编号
     */
    @RequestMapping(value = "/selectRooms", method = RequestMethod.POST)
    public Wrapper selectRooms(Long nursingGroupId) {

        String msg_comment_query_param_error = cmnyMessageSource.getMessage("comment.query.param.error", null);
        String msg_comment_query_success = cmnyMessageSource.getMessage("comment.query.success", null);
        String msg_group_room_not_exist = cmnyMessageSource.getMessage("group.room.not.exist", null);

        if (null == nursingGroupId || nursingGroupId <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, msg_comment_query_param_error, null);
        }
        List<Room> list = groupService.selectNursingBindRoom(nursingGroupId);
        if (CollectionUtils.isEmpty(list)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, msg_group_room_not_exist, null);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, msg_comment_query_success, list);
    }

    /**
     * 床位视图——平板端
     */
    @RequestMapping(value = "/bedViews", method = RequestMethod.POST)
    public Wrapper bedViews(Long nursingGroupId) {

        String msg_comment_query_param_error = cmnyMessageSource.getMessage("comment.query.param.error", null);
        String msg_group_room_not_exist = cmnyMessageSource.getMessage("group.room.not.exist", null);
        String msg_comment_query_success = cmnyMessageSource.getMessage("comment.query.success", null);

        if (null == nursingGroupId) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, msg_comment_query_param_error, 0);
        }
        //获取该组负责的房间
        List<Room> list = groupService.selectNursingBindRoom(nursingGroupId);
        if (CollectionUtils.isEmpty(list)) {//        	return new JsonResult(false,"该护工组还未分配房间",0);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, msg_group_room_not_exist, 0);
        } else {
            for (int i = 0; i <= list.size() - 1; i++) {
                List<Bed> bedViewList = groupService.selectByRoomId(list.get(i).getRoomId().longValue());//获取房间对应的床位和老人信息集合
                list.get(i).setBedList(bedViewList);//设置集合值
            }
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, msg_comment_query_success, list);
    }


    /**
     * 获取登录用户详情(增加查询是否是班长,班组名,班组ID)
     *
     * @return
     */
    @RequestMapping(value = "/getUserLongDetail", method = RequestMethod.POST)
    public Wrapper getUserLongDetail() {
        Long userId = getLoginAuthDto().getUserId();
        Long groupId = getLoginAuthDto().getGroupId();
        OmcUserDetailVo omcUserDetailVo = groupService.queryUserDetail(userId, groupId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), omcUserDetailVo);
    }

    /*
     * 大数据平台健康占比亚健康展示
     * */
    @RequestMapping(value = "/selectByNursingHome",method = RequestMethod.POST)
    public Wrapper selectByNursingHome(){
        Long groupId = getLoginAuthDto().getGroupId();

        NurseHomeVo nurseHomeVo = groupService.selectByNursingHome(groupId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),nurseHomeVo);
    }

    /**
     * 护工绑定房间
     */
    @PostMapping("/nursingBindRoom")
    @Transactional
    public Wrapper nursingBindRoom(@RequestBody NursingBindRoomDto nursingBindRoomDto){
        //判断传参nursingBindRoomDto
        if(nursingBindRoomDto == null || nursingBindRoomDto.getUserIds() == null){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //护工绑定护工组
        //groupService.deleteByNursingGroupId(nursingBindRoomDto);
        groupService.nursingBindNursingGroup(nursingBindRoomDto);

        if(nursingBindRoomDto.getUserIds().size() > 0){
            groupService.deleteByUserId(nursingBindRoomDto.getUserIds());
        }
        //判断房间为空
        if(nursingBindRoomDto.getRoomIds().size() < 1){
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                    .getMessage("operation.success", null));
        }
        Integer result = groupService.nursingBindRoom(nursingBindRoomDto);

        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /*
    * 查询班组当前负责的房间以及班次
    * */
    @PostMapping("/queryRoomAndNursing")
    public Wrapper queryRoomAndNursing(){
        return null;
    }

}
